package com.example.tass.licence;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

public class KeyTools {

    private static final String BASE_FILE_PATH = "D:\\a_myFile\\dinglian\\other\\licence";
    //private static final String BASE_FILE_PATH = "/opt/licence";

    //Cer证书存放路径
    public static final String CER_FILE_PATH = BASE_FILE_PATH + "\\publicCer.cer";
    //public static final String CER_FILE_PATH = BASE_FILE_PATH + "/publicCer.cer";

    /**
     * 通过 cer证书获取公钥
     */
    public static PublicKey getPublicKeyFromCer() {
        PublicKey publicKey = null;
        FileInputStream in = null;
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            in = new FileInputStream(CER_FILE_PATH);
            Certificate c = cf.generateCertificate(in);
            publicKey = c.getPublicKey();
        } catch (CertificateException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return publicKey;
    }
}


